import com.example.study_mybatis.mapper.AddressMapper;
import com.example.study_mybatis.mapper.UserMapper2;
import com.example.study_mybatis.model.Address;
import com.example.study_mybatis.model.PageInfo;
import com.example.study_mybatis.model.User;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserTest2 {
    private UserMapper2 userMapper2;
    private UserMapper2 userMapper22;
    private AddressMapper addressMapper;

    private SqlSession sqlSession;
    private SqlSession sqlSession2;

    SqlSessionFactory sqlSessionFactory;
    @Before
    public void before() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        // 设置为自动提交了
//        sqlSession = sqlSessionFactory.openSession(true);
        // 涉及到多次写操作的时候，需要开启事务
        // 打开session的时候可以指定隔离级别,根据不同的场景选择合适的事务隔离级别
//        sqlSession = sqlSessionFactory.openSession(TransactionIsolationLevel.READ_UNCOMMITTED);
//        sqlSession = sqlSessionFactory.openSession(TransactionIsolationLevel.READ_COMMITTED);
//        sqlSession = sqlSessionFactory.openSession(TransactionIsolationLevel.REPEATABLE_READ);
//        sqlSession = sqlSessionFactory.openSession(TransactionIsolationLevel.SERIALIZABLE);
        sqlSession = sqlSessionFactory.openSession();
        sqlSession2 = sqlSessionFactory.openSession();
        userMapper2 = sqlSession.getMapper(UserMapper2.class);
        userMapper22 = sqlSession2.getMapper(UserMapper2.class);
        addressMapper = sqlSession.getMapper(AddressMapper.class);
    }

    @After
    public void after() {
//        sqlSession.commit();
//        sqlSession.close();
    }

    @Test
    public void listByCondition() {
        // $ 可能存在sql注入的风险的
//        String username = "三' or nickname like '%";
        String username = "三";
        // 字符串最好在后台生成，不要让用户传入进来
        List<User> list = userMapper2.listByCondition(username, "order by age asc");
        System.out.println(list.size());
        // # 和 $
        // concat连接字符串
        // @Param注解
    }

    @Test
    public void listByUser() {
        User user = new User();
        user.setNickname("张");
        user.setUsername("张三");
        List<User> list = userMapper2.listByUser(user);
        System.out.println(list.size());
    }

    @Test
    public void listByUser2() {
        User user = new User();
//        user.setNickname("张");
//        user.setUsername("张三");
//        user.setId(13);
        List<User> list = userMapper2.listByUser2(user);
        System.out.println(list.size());
    }

    @Test
    public void listByUserOrderBy() {
        User user = new User();
        user.setUsername("张三");
        List<User> list = userMapper2.listByUserOrderBy(user, "order by id desc");
        System.out.println(list.size());
    }

    @Test
    public void update() {
        User user = new User();
        user.setId(13);
        user.setAge(10);
        user.setUsername("王五");
        user.setNickname("测试一下");
        int i = userMapper2.update2(user);
        assert i == 1;
    }

    @Test
    public void batchInsert() {
        List<User> userList = new ArrayList<>();
        userList.add(new User("admin", "123456", "张三", 18, new Date(), "河北科技小大"));
        userList.add(new User("admin2", "123456", "张三2", 18, new Date(), "河北科技小大"));
        userList.add(new User("admin3", "123456", "张三3", 18, new Date(), "河北科技小大"));
        int i = userMapper2.batchInsert(userList);
        assert i == userList.size();
    }

    @Test
    public void batchDelete() {
        List<Integer> list = new ArrayList<>();
        list.add(16);
        list.add(19);
        list.add(20);
        list.add(21);
        int i = userMapper2.batchDelete(list);
        assert i == list.size();
    }

    @Test
    public void selectById() {
        User user = userMapper2.selectById(13);
        System.out.println("=========================");
        System.out.println(user.getAddressList().size());
    }

    @Test
    public void selectById2() {
        User user = userMapper2.selectById2(13);
        System.out.println(user.getAddressList().size());
    }

    @Test
    public void selectById3() {
        User user = userMapper2.selectById3(13);
        System.out.println(user.getAddressList().size());
    }

    @Test
    public void listWithAddress() {
        List<User> list = userMapper2.listWithAddress();
    }


    @Test
    public void insertUser() {
        // 插入用户和对应地址
        try {
            User user = new User();
            user.setNickname("天亮");
            Address address = new Address();
            address.setAddress("河北科技大学");
            // 先插入用户,需要获取刚插入的这条用户的id
            userMapper2.insert(user);
            int i = 10 / 0;
            System.out.println(user.getId());
            address.setUserId(user.getId());
            addressMapper.insert(address);
        } catch (Exception e) {
            // 进行事务回滚
            sqlSession.rollback();
        }

    }

    @Test
    public void sel() {
        List<User> list = userMapper2.selectByUsername("张三");
    }

    @Test
    public void cache() {
        // 一级缓存，mybatis默认开启一级缓存，当我们在同一个sqlSession中重复会执行同一条sql语句的时候，
        // 后续的内容会从缓存中读取
        User user = userMapper2.selectById(13);
        // 清除1级缓存
//        sqlSession.clearCache();
        System.out.println("========================");
        User user2 = userMapper2.selectById(13);
        System.out.println("========================");
        User user3 = userMapper22.selectById(13);
    }

    @Test
    public void cache2() {
        // 一级缓存，mybatis默认开启一级缓存，当我们在同一个sqlSession中重复会执行同一条sql语句的时候，
        // 后续的内容会从缓存中读取
        userMapper2.list();
        // 清除1级缓存
//        sqlSession.clearCache();
        System.out.println("========================");
        userMapper2.list();
        sqlSession.clearCache();
        sqlSession.close();
        sqlSessionFactory.getConfiguration().getCache("com.example.study_mybatis.mapper.UserMapper2").clear();
        System.out.println("========================");
        // 只有之前的sqlSession关闭之后才可以生效
        userMapper22.list();
    }

    @Test
    public void page() {
        PageInfo<User> pageInfo = new PageInfo<>();
        pageInfo.setTotal(userMapper2.count());
        pageInfo.setList(userMapper2.page(pageInfo.getStart(),pageInfo.getPageSize()));
        System.out.println(pageInfo);

        pageInfo.setPageNum(pageInfo.getPageNum() + 1);
        pageInfo.setTotal(userMapper2.count());
        pageInfo.setList(userMapper2.page(pageInfo.getStart(),pageInfo.getPageSize()));
        System.out.println(pageInfo);
    }

    @Test
    public void page2() {
        PageHelper.startPage(2, 5);
        List<User> userList = userMapper2.list();
        List<User> userList2 = userMapper2.list();

        com.github.pagehelper.PageInfo<User> pageInfo= com.github.pagehelper.PageInfo.of(userList);
        // 这个pageInfo就是插件给我们提供的分页信息
    }
}
